﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections;
using System.Linq;
using ScrumTable.BL.DM.Properties;
using ScrumTable.Common;
using ScrumTable.Common.Collections;
using ScrumTable.DL.Data.Schema;

#endregion

namespace ScrumTable.BL.DM.DataManagement
{

    /// <summary>
    /// Contains the basic information about a user story.
    /// </summary>
    public class UserStory : ScrumItemBase
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private DomainCollection<Task> _tasks;
        private DomainCollection<Bug> _bugs;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets a list of all tasks associated to the current user story.
        /// </summary>
        public DomainCollection<Task> Tasks
        {
            get
            {
                if (_tasks == null)
                {
                    _tasks = new DomainCollection<Task>(
                        false,
                        Context,
                        this,
                        ContextInternal.Data.Storage[Schema.TaskSchema]);
                }
                return _tasks;
           }
        }

        /// <summary>
        /// Gets a list of all bugs.
        /// </summary>
        public DomainCollection<Bug> Bugs
        {
            get
            {
                if (_bugs == null)
                {
                    _bugs = new DomainCollection<Bug>(
                        false,
                        Context,
                        this,
                        ContextInternal.Data.Storage[Schema.BugSchema]);
                }
                return _bugs;
            }
        }


        /// <summary>
        /// Specifies the story points assigned to the current user story.
        /// </summary>
        public double? StoryPoints
        {
            get { return DataObject.GetData(Schema.UserStorySchema.StoryPoints, data => (double)data, new double?()); }
            set { SetDataWithUndoAction(Schema.UserStorySchema.StoryPoints, value); }
        }

        /// <summary>
        /// Gets the ScrumItemType of the ScrumItem
        /// </summary>
        public override ScrumItemType ItemType
        {
            get { return ParentProject.Types.UserStoryType; }
        }

        /// <summary>
        /// Gets the parent collection instance.
        /// </summary>
        internal override IDomainCollection ParentCollection
        {
            get { return Parent.SearchCollection<UserStory>(); }
        }

        /// <summary>
        /// Gets a collection which contains all child collections of the current
        /// domain object.
        /// </summary>
        internal override ICollection ChildCollections
        {
            get
            {
                ArrayList childCols = new ArrayList(base.ChildCollections);
                childCols.Add(Tasks);
                childCols.Add(Bugs);
                return childCols;
            }
        }


        /// <summary>
        /// Gets the domain object schema of the current domain object.
        /// </summary>
        protected override SchemaBase DomainSchema { get { return Schema.UserStorySchema; } }

        /// <summary>
        /// Returns the name-field schema information.
        /// </summary>
        protected override SchemaBase NameSchema { get { return Schema.UserStorySchema.Name; } }

        /// <summary>
        /// Returns the description-field schema information.
        /// </summary>
        protected override SchemaBase DescriptionSchema { get { return Schema.UserStorySchema.Description; } }

        /// <summary>
        /// Returns the memberid-field schema information.
        /// </summary>
        protected override SchemaBase AssignedMemberSchema { get { return Schema.UserStorySchema.MemberId; } }

        /// <summary>
        /// Returns the ranking-field schema information.
        /// </summary>
        protected override SchemaBase RankingSchema { get { return Schema.UserStorySchema.Ranking; } }

        /// <summary>
        /// Returns the state-field schema information.
        /// </summary>
        protected override SchemaBase StateSchema { get { return Schema.UserStorySchema.State; } }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Returns true if the specified state change could be accomplished.
        /// </summary>
        /// <param name="newState">Specifies the new state to validate (to).</param>
        public override bool CanChangeState(ScrumState newState)
        {
            return (base.CanChangeState(newState) && ParentProject.Types.UserStoryType.CanChangeState(State, newState));
        }

        /// <summary>
        /// Resets all child collections (IDomainCollection instances) of the
        /// current domain object.
        /// </summary>
        protected override void ResetChildCollections()
        {
            base.ResetChildCollections();

            _tasks = null;
            _bugs = null;
        }

        /// <summary>
        /// Initializes the mappings stored in the current instance.
        /// </summary>
        protected override void InitializeMappings()
        {
            base.InitializeMappings();

            PropertySchemaMappings.Add(Schema.UserStorySchema.StoryPoints, "StoryPoints", true);
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}